﻿using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using ISSMoviePlex.BLL;

namespace ISSMoviePlex.Service
{
    //*************************************CONCURRENCY************************************/
    //K: We need to set ConcurrencyMode to Single to ensure THREAD SAFETY (avoiding many threads booking one movie ticket)
    //*************************************INSTANCE**************************************/
    //K: In our case, we should use Single because all clients can access the service via ONE instance without any issue
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
        ConcurrencyMode = ConcurrencyMode.Single)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    //IIS(8.0) URL: http://localhost/CineService/TicketBookingService.svc
    public class TicketBookingService : ITicketBookingService
    {
        private readonly TicketBookingBLL _ticketBookingLogic = new TicketBookingBLL();
        private readonly ScheduleBLL _scheduleLogic = new ScheduleBLL();

        public ScheduleCollection GetSchedules(string movieName)
        {
            var scheduleList = new ScheduleCollection();
            var schedulesOfMovie = _scheduleLogic.GetScheduleByMovieName(movieName);
            scheduleList.AddRange(schedulesOfMovie.Select(schedule => new Schedule()
            {
                ScheduleId = schedule.ScheduleId,
                SeatAvailable = schedule.SeatAvailable,
                ShowTime = schedule.ShowTime,
                MovieId = schedule.MovieId
            }));
            return scheduleList;
        }

        public void BookTicket(int noOfTicketsBooking, int scheduleId, int memberId)
        {
            double discount;
            _ticketBookingLogic.PerformBooking(noOfTicketsBooking, scheduleId, memberId, out discount);
        }
    }
}
